#include "PairingFunction.h"

// SOURCE: http://hbfs.wordpress.com/2011/09/27/pairing-functions/
// Takes two numbers x and y and returns a unique integer
uint64_t encode (uint32_t x, uint32_t y) {
  int64_t p=0;
  int i=0;

  while (x||y) {
    p|= ((uint64_t)(x&1)<<i);     x>>=1;
    p|= ((uint64_t)(y&1)<<(i+1)); y>>=1;
    i+=2;
  }
  return p;
}

// SOURCE: http://hbfs.wordpress.com/2011/09/27/pairing-functions/
// Takes a number p and generates two numbers x and y
void decode (uint64_t p, uint32_t & x, uint32_t & y) {
  x=0;
  y=0;
  int i=0;
  while (p) {
    x|=((uint32_t)(p&1)<<i); p>>=1;
    y|=((uint32_t)(p&1)<<i); p>>=1;
    i++;
  }
}
